package android.framework.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBHelperUtil {

	private Context sqliteContext;
	private DBHelper dbHelper;
	private SQLiteDatabase mSqliteDB = null;

	public DBHelperUtil(Context context) {
		super();
		this.sqliteContext = context;
		this.dbHelper = new DBHelper(context);
		mSqliteDB = dbHelper.getWritableDatabase();
	}

	public void openDb() {
		if (mSqliteDB == null || !mSqliteDB.isOpen()) {
			mSqliteDB = dbHelper.getWritableDatabase();
		}
	}

	public void closeDb() {
		if (mSqliteDB != null && mSqliteDB.isOpen()) {
			mSqliteDB.close();
		}
	}

	/** 
	* @Title: insert 
	* @Description: TODO
	* @param table
	* @param nullColumnHack
	* @param values
	* @return long   
	* @throws TODO
	*/ 
	public long insert(String table, String nullColumnHack, ContentValues values) {
		long l = mSqliteDB.insert(table, nullColumnHack, values);
		return l;
	}

	/** 
	* @Title: delete 
	* @Description: TODO
	* @param table
	* @param whereClause
	* @param whereArgs
	* @return int   
	* @throws TODO
	*/ 
	public int delete(String table, String whereClause, String[] whereArgs) {
		int i = mSqliteDB.delete(table, whereClause, whereArgs);
		return i;
	}

	/** 
	* @Title: query 
	* @Description: TODO
	* @param table
	* @param columns
	* @param selection
	* @param selectionArgs
	* @param groupBy
	* @param having
	* @param orderBy
	* @param limit
	* @return Cursor   
	* @throws TODO
	*/ 
	public Cursor query(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy, String limit) {
		Cursor cur = mSqliteDB.query(table, columns, selection, selectionArgs,
				groupBy, having, orderBy, limit);
		return cur;
	}

	/** 
	* @Title: update 
	* @Description: TODO
	* @param table
	* @param values
	* @param whereClause
	* @param whereArgs
	* @return int   
	* @throws TODO
	*/ 
	public int update(String table, ContentValues values, String whereClause,
			String[] whereArgs) {
		int i = mSqliteDB.update(table, values, whereClause, whereArgs);
		return i;
	}

}
